var gcommon = {
    /**
     * 用户
     */
    u: function() {
        var userinfo = localStorage.getItem('local-userinfo');
        if(userinfo) {
            userinfo = JSON.parse(userinfo);
            return userinfo;
        }
        return null;
    },

    /**
     * 用户ID
     */
    uid: function() {
        var userinfo = localStorage.getItem('local-userinfo');
        if(userinfo) {
            userinfo = JSON.parse(userinfo);
            return userinfo.id;
        }
        return null;
    }
};

function g_auth() {
    if(!gcommon.uid() && location.href.indexOf('/bd/') === -1 ) {
        if(location.href.indexOf('/login') !== -1) {
            return;
        }
        var openid = gutils.getParameter('openid');
        var nickname = gutils.getParameter('nickname');
        var avatar = gutils.getParameter('avatar');

//         openid = Date.now(); // TODO

        if(!openid) {
            // 授权
            window.location.href = gconsts.wx.domain +
                    '/oauth?appid=' + gconsts.wx.appid +
                    '&scope=snsapi_userinfo'; // snsapi_base
        } else {
            // 登陆
            ghttp.post('/login', {
                openid: openid,
                nickname: nickname? decodeURIComponent(nickname): nickname,
                avatar: avatar? decodeURIComponent(avatar): avatar
            }, function(resp) {
                if(resp.success) {
                    var u = resp.result;
                    localStorage.setItem('local-userinfo', JSON.stringify(u));
                    window.location.reload();
                }
            });
        }
    }
}

function g_count() {
    var localCount = localStorage.getItem('local-basket-count');
    if(localCount) {
        setTimeout(function() {
            if(localCount *1 > 0) {
                $('.badget').text(localCount).show();
            }
        }, 500);
    } else {
        if(gcommon.uid() && location.href.indexOf('/bd/') === -1 ) {
            ghttp.get('/personal/basket/count/' + gcommon.uid(), null, function(resp) {
                if(resp.success) {
                    if(resp.result.c *1 > 0) {
                        localStorage.setItem('local-basket-count', resp.result.c);
                        setTimeout(function () {
                            $('.badget').text(resp.result.c).show();
                        }, 500);
                    }
                }
            });
        }
    }
}


/**
 * 获取商品图片
 */
function g_image(name) {
    var cid = 0;
    switch (name) {
        case '五花肉（肋条）': {
            cid = 900012;
            break;
        }
        case '鲫鱼': {
            cid = 900011;
            break;
        }
        case '番茄': {
            cid = 92;
            break;
        }
        case '长白萝卜': {
            cid = 52;
            break;
        }
        case '牛肉': {
            cid = 900010;
            break;
        }
        case '鸡蛋': {
            cid = 90006;
            break;
        }
        case '黄瓜': {
            cid = 114;
            break;
        }
        case '青椒': {
            cid = 102;
            break;
        }
        case '土豆': {
            cid = 62;
            break;
        }
        case '鸡毛菜': {
            cid = 18;
            break;
        }
        case '米苋': {
            cid = 15;
            break;
        }
        case '芹菜': {
            cid = 4;
            break;
        }
        case '大白菜': {
            cid = 2;
            break;
        }
        case '青菜': {
            cid = 1;
            break;
        }
        case '草鱼': {
            cid = 'caoyu';
            break;
        }
        case '猪大排': {
            cid = 'zhudapai';
            break;
        }
        case '茄子': {
            cid = 'qiezi';
            break;
        }
        case '花菜': {
            cid = 'huacai';
            break;
        }
        case '猪后腿肉': {
            cid = 'zhuhoutuirou';
            break;
        }
        case '冬瓜': {
            cid = 'donggua';
            break;
        }
        case '带鱼': {
            cid = 'daiyu';
            break;
        }
    }
    return 'url(/images/' + cid + '.jpg)';
}


$(function() {
    // 授权
   // g_auth();

    // 购物篮数量
    g_count();

    // 微信JS SDK初始化
    wxsdk.initConfig(function() {
        var title = '普陀区每日菜价';
        var content = '普陀区的小伙伴们有福啦，在这里可以查询到当前区内各菜场实时菜价，还有菜谱可以选择哦，快来试试吧';
        var url = gconsts.wx.domain;
        var icon = url + '/icons/share.jpg';
        wxsdk.onShareTimeline(title, icon, url);
        wxsdk.onShareAppMessage(title, content, icon, url);
    });
}());

